/**
 * @file 路由跳转布置
 * @author 叶永鸿
 * @description 页面跳转所需的路由依赖
 * @date 2021-7-11
 * @LastEditors 叶永鸿
 * @LastEditTime 2021-7-28
 */
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { useStore } from "vuex";
import { LoginState } from "@/store";
import store from '@/store';






// import emSignUp from '../components/emSignUp.vue'
// import hrSignUp from '../components/hrSignUp.vue'

// //import login from '../views/login.vue'
// import signUp from '../views/signUp.vue'



const routes: Array<RouteRecordRaw> = [
  {
    path: '/', redirect: '/public'
  },
  {

    path:'/public',component:()=>import("../views/public.vue"),
    
  },
  {
    name: 'login',
    path: '/login', component: () => import("../views/loginRegister.vue"),
    meta:{
      loginRequired:false,
    }

  },
  {

    path: '/hrMain', component: () => import('../components/hrMain.vue'),
    meta: {
      loginRequired: true,
      hrRequired: true,
    },
    children: [
      {
        name: 'search',
        path: 'search', component: () => import('../components/search.vue'),
        meta: {
          loginRequired: true,
          hrRequired: true,
        },
      },
      {
        path: 'logEmp', component: () => import("../components/logEmp.vue"),
        meta: {
          loginRequired: true,
          hrRequired: true,
        },
      },
      {
        name: 'add',
        path: 'add', component: () => import('../components/add.vue'),
        meta: {
          loginRequired: true,
          hrRequired: true,
        },
      },
      {
        name: 'manage',
        path: 'manage', component: () => import('../components/manage.vue'),
        meta: {
          loginRequired: true,
          hrRequired: true,
        },
      },
      {
        name: 'searchPubInfor',
        path: 'searchPubInfor', component: () => import('../components/searchPubInfor.vue'),
        props: true,
        meta: {
          loginRequired: true,
          hrRequired: true,
        },
      },
      {
        name: 'manageInfor',
        path: 'manageInfor', component: () => import('../components/manageInfor.vue'),
        props: true,
        meta: {
          loginRequired: true,
          hrRequired: true,
        },
      }

    ]
  },
  {
    path: '/emMain', component: () => import("../components/emMain.vue"),
    meta: {
      loginRequired: true,
      emRequired: true,
    },
    children: [
      {
        name: "myself", path: "myself", component: () => import("../components/myself.vue"),
        meta: {
          loginRequired: true,
          emRequired: true,
        },
      }
    ]
  },

  {
    path: '/:catchAll(.*)',
    name: '/404',
    component: () => import('../views/404.vue')
  }

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  //console.log(from);
  var isLogin  =  store.getters.isLogined;
  var isHr = store.getters.isHr;
  var isEm = store.getters.isEm;
  //var isLogin = false;
  if(to.path=="/public"){
    next();
  }
  else if (!isLogin && to.matched.some(function (item) {
    // return item.path == '/hrMain';
    if (item.meta.loginRequired) {
      return true;
    }
  })) {
    next('/login');
  }else if(isLogin && to.matched.some(function(item){
    if(!item.meta.loginRequired){
      return true;
    }
  })) {
    if(isHr){
      next('/hrMain')
    }else{
      next('/emMain')
    }
  }
  else if(!isHr && to.matched.some(function (item){
    if(item.meta.hrRequired){
      return true;
    }
  })){
    next('/emMain');
  }  else if(!isEm&& to.matched.some(function(item){
    if(item.meta.emRequired){
      return true;
    }
  })){
    next('/hrMain');
  } else{
    next();
  }

})

export default router
